package cn.melon.view.admin.store;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import cn.melon.mod.store.StoreService;
import cn.melon.mod.store.StoreServiceImpl;
import cn.melon.mod.store.entity.Goods;
import cn.melon.mod.store.entity.GoodsTable;
import cn.melon.view.admin.store.ctr.StorePanelCtr;
import cn.melon.view.admin.store.ctr.StoreTableMouse;

public class StoreJPanel extends JPanel implements UpdateGoodsTable {
    /**
     * 
     */
    /*
     * 监听器
     */
    private ActionListener listener = new StorePanelCtr(this);
    private StoreTableMouse mouseListener = new StoreTableMouse(this);
    
    /*
     * 
     */
    private StoreService storeService = new StoreServiceImpl();

    
    private static final long serialVersionUID = 1L;
    private String goodsTypess[] = { "全部", "日用百货", "学习用具", "零食饮料", "学习资料" };
    private JComboBox<String> goodsType;

    private JTextField Id;
    private JTextField name;
    private JButton queryButton;
    private JButton addButton;

    private String[] tableColName = { "商品编号", "商品名称", "商品类别", "商品价格","商品描述", "商品销量", "是否售空" };
    private JTable table;
    private DefaultTableModel tableModel;

    /**
     * Create the panel.
     */
    public StoreJPanel() {
        initialize();
        storeService.getGoodsTable(this,this.getGoodsType(), 0, 30,"","");
    }

    private void initialize() {
        this.setSize(1200, 800);
        // this.setBackground(Color.blue);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JPanel topPanel = new JPanel();
        topPanel.setSize(1200, 50);
        topPanel.setBackground(Color.darkGray);

        JPanel tyPanel = new JPanel();
        JLabel tyText = new JLabel("商品类别:");
        goodsType = new JComboBox<String>(goodsTypess);
        tyPanel.add(tyText);
        tyPanel.add(goodsType);

        JPanel goodsId = new JPanel();
        JLabel goodsIdText = new JLabel("商品编号:");
        Id = new JTextField("",10);
        goodsId.add(goodsIdText);
        goodsId.add(Id);

        JPanel goodsName = new JPanel();
        JLabel nameText = new JLabel("商品名称:");
        name = new JTextField("",10);
        goodsName.add(nameText);
        goodsName.add(name);

        queryButton = new JButton("查询");
        addButton = new JButton("上架");
        queryButton.addActionListener(listener);
        addButton.addActionListener(listener);

        topPanel.add(Box.createRigidArea(new Dimension(200, 15)));
        topPanel.add(tyPanel);
        topPanel.add(goodsId);
        topPanel.add(goodsName);
        topPanel.add(queryButton);
        topPanel.add(Box.createRigidArea(new Dimension(300, 15)));
        topPanel.add(addButton);
        this.add(topPanel);

        JScrollPane tablePanel = new JScrollPane();
        tablePanel.setPreferredSize(new Dimension(360, 650));
        tableModel = new DefaultTableModel();
        table = new JTable(tableModel) {
            private static final long serialVersionUID = 1L;

            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.addMouseListener(mouseListener);
        for (int i = 0; i < tableColName.length; i++) {
            tableModel.addColumn(tableColName[i]);
        }
        tablePanel.setViewportView(table);

        this.add(tablePanel);

    }

    public String getGoodsType() {
        return (String) goodsType.getSelectedItem();
    }

    public String getGoodsId() {
        return Id.getText();
    }

    public String getGoodsName() {
        return name.getText();
    }
    
    public Goods getTableRow(){
        int row = table.getSelectedRows()[0];
        Goods goods = new Goods((String)tableModel.getValueAt(row, 0), (String)tableModel.getValueAt(row, 1), 
                (String)tableModel.getValueAt(row, 2), Double.parseDouble((String) tableModel.getValueAt(row, 3)), 
                (String)tableModel.getValueAt(row, 4), Long.parseLong((String)tableModel.getValueAt(row, 5)), 
                Boolean.parseBoolean((String)tableModel.getValueAt(row, 6)));
        return goods;
    }
    /*
     * (non-Javadoc)
     * 
     * @see cn.melon.view.admin.updateView#updateAll(cn.melon.mod.store.entity.
     * GoodsTable)
     */
    @Override
    public void updateTable(GoodsTable goodsTable) {

        /*
         * 删除全部行
         */
        int rows = tableModel.getRowCount();
        for (int i = 0; i < rows; i++) {
            tableModel.removeRow(0);
        }

        /*
         * 添加信息
         */
        int goodsNum = goodsTable.length();
        String[] strings = new String[7];
        for (int i = 0; i < goodsNum; i++) {
            Goods goods = goodsTable.get(i);
            strings[0] = goods.getGoodsId();
            strings[1] = goods.getGoodsName();
            strings[2] = goods.getGoodsType();
            strings[3] = String.valueOf(goods.getGoodsPrice());
            strings[4] = goods.getGoodsDescription();
            strings[5] = String.valueOf(goods.getGoodsSales());
            strings[6] = String.valueOf(goods.isSoldOut());
            tableModel.addRow(strings);
        }

    }
}
